package cn.jzq.kotoba.modules.designpattern.observer.weather;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jzq
 * @date 2018/12/19 14:08
 */
@Data
public class WeatherData implements Subject {

    private List<Observer> observers;

    private Double temperature;

    private Double humidity;

    private Double pressure;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

    @Override
    public void registerObject(Observer observer) {
        observers.add(observer);
        System.out.println("register observer");
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
        System.out.println("remove observer");
    }

    @Override
    public void notifyObserver() {
        for (Observer observer: observers) {
            observer.update(temperature, humidity, pressure);
        }
        System.out.println("notify observer");
    }

    public void measurementsChanged() {
        notifyObserver();
    }

    public void setMeasurements(Double temperature, Double humidity, Double pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    @Override
    public String toString() {
        return "WeatherData{" +
                "observers=" + observers +
                ", temperature=" + temperature +
                ", humidity=" + humidity +
                ", pressure=" + pressure +
                '}';
    }
}
